<template>
    <div style="margin-top:200px;margin-left: 500px;"> <el-form ref="ruleFormRef" style="max-width: 600px"
            :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>


            <el-form-item label="用户名" prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item label="密码" prop="pwd">
                <el-input v-model="ruleForm.pwd" show-password />
            </el-form-item>

            <el-form-item label="验证码" prop="ycode">
                <el-input v-model="ruleForm.scode" placeholder="请输入验证码" style="width: 200px; margin-right: 20px;" />
                <el-tag>{{ ruleForm.kcode }}</el-tag> <el-button type="danger" @click="setCode()">点击切换</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">
                    登录
                </el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </div>

</template>


<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import axios from 'axios';
import router from '@/router';

const setCode = () => {
    let str = '123456789abcdefghijiklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ';
    let code = ''
    for (var i = 0; i < 6; i++) {
        var random = Math.floor(Math.random() * str.length)
        code = code + str[random];
    }

    ruleForm.kcode = code

}

interface RuleForm {
    name: string
    pwd: string
    kcode: string //用户看到的
    scode: string //用户输入的验证码

}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
    name: '',
    pwd: '',
    kcode: '',
    scode: ''

})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
    name: [
        { required: true, message: '请输入用户名!', trigger: 'blur' },
        { min: 1, max: 10, message: '长度', trigger: 'blur' },
    ],
    pwd: [
        { required: true, message: '请输入密码!', trigger: 'blur' },
        { min: 5, max: 10, message: '密码长度为5~10位', trigger: 'blur' },
    ]
})

const submitForm = async (formEl: FormInstance | undefined) => {

    if (ruleForm.kcode.toLocaleLowerCase() != ruleForm.scode.toLocaleLowerCase()) {

        ElMessage.error("请输入正确的验证码"); return;
    }
    setCode();
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {//在这里写登录逻辑

            axios.get('https://localhost:7249/api/Account/Login', { params: { AccountName: ruleForm.name, AccountPass: ruleForm.pwd } })
                .then(res => {
                    console.log(res);
                    if (res.data.code == 201) {
                        ElMessage.success(res.data.msg);
                        router.push("main");
                    } else {
                        ElMessage.error(res.data.msg);
                    }
                });

        } else {
            console.log('error submit!', fields)
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
}))
//钩子函数 生命周期函数
onMounted(() => {
    setCode();
});
</script>